﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;

namespace ElectronicCommerce.Framework.Web.UI.Controls
{
    /// <summary>
    /// 
    /// </summary>
    public class PageHeadView : ServerControlBase
    {
        /// <summary>
        /// Sends server control content to a provided <see cref="T:System.Web.UI.HtmlTextWriter"/> object, which writes the content to be rendered on the client.
        /// </summary>
        /// <param name="writer">The <see cref="T:System.Web.UI.HtmlTextWriter"/> object that receives the server control content.</param>
        protected override void Render(HtmlTextWriter writer)
        {
            StringBuilder headInfo = new StringBuilder();

            if (base.PageHead != null)
            {
                //title
                if (!string.IsNullOrEmpty(base.PageHead.Title))
                {
                    headInfo.Append("\t<title>");
                    headInfo.Append(base.PageHead.Title);
                    headInfo.Append("</title>");
                }

                // http equiv meta
                foreach (PageHeadMetaInfo metaInfo in base.PageHead.GetHttpEquivMetas())
                {
                    headInfo.Append("\r\t<meta http-equiv=\"" + metaInfo.Name + "\" content=\"" + metaInfo.Content + "\" />");
                }

                // name meta
                foreach (PageHeadMetaInfo metaInfo in base.PageHead.GetNameMetas())
                {
                    headInfo.Append("\r\t<meta name=\"" + metaInfo.Name + "\" content=\"" + metaInfo.Content + "\" />");
                }

                // icon
                if (!string.IsNullOrEmpty(base.PageHead.Icon))
                {
                    headInfo.Append("\r\t<link rel=\"shortcut icon\" type=\"image/x-icon\" href=\"" + PageHead.Icon + "\" />");
                }

                // rss feeds
                foreach (RssFeedItem rssFeeditem in base.PageHead.RssFeedList)
                {
                    headInfo.Append("\r\t<link rel=\"alternate\" type=\"application/rss+xml\" title=\"" + rssFeeditem.Title + "\" href=\"" + rssFeeditem.Url + "\" />");
                }

                headInfo.Append(BuildCss(base.PageHead.GetCssFiles()));
                headInfo.Append(BuildCss(base.PageHead.GetInlineStyles()));

                headInfo.Append(BuildJavascript(base.PageHead.GetJavascriptFiles()));
                headInfo.Append(BuildJavascript(base.PageHead.GetInlineJavascripts()));
            }

            writer.WriteLine(headInfo.ToString());
        }

        #region Private

        /// <summary>
        /// Builds the CSS.
        /// </summary>
        /// <param name="cssFileList">The CSS file list.</param>
        /// <returns></returns>
        private string BuildCss(List<CssItem> cssFileList)
        {
            StringBuilder sb = new StringBuilder();

            if (cssFileList != null && cssFileList.Count != 0)
            {
                string styleTagHeader = string.Empty;
                string styleTagFooter = string.Empty;
                string version = string.Empty;
                string content = string.Empty;
                foreach (CssItem item in cssFileList)
                {
                    content = item.Content;
                    version = (item.BrowserVersion > 0) ? item.BrowserVersion.ToString() : "";
                    switch (item.ConditionalCommentType)
                    {
                        case ConditionalCommentType.IEOnly:
                            styleTagHeader = "<!--[if IE" + version + "]>";
                            styleTagFooter = "<![endif]-->";
                            break;
                        case ConditionalCommentType.NonIEOnly:
                            styleTagHeader = "<![if IE" + version + "]>";
                            styleTagFooter = "<![endif]>";
                            break;
                        default:
                            styleTagHeader = string.Empty;
                            styleTagFooter = string.Empty;
                            break;
                    }

                    sb.Append("\r\t");

                    if (item.Type == CssType.External)
                    {
                        styleTagHeader += "<link rel=\"stylesheet\" type=\"text/css\" href=\"";
                        styleTagFooter = "\" />" + styleTagFooter;
                        content = base.Resource.BuildCss(item.Content);
                    }

                    sb.Append(styleTagHeader);
                    sb.Append(content);
                    sb.Append(styleTagFooter);
                }
            }

            return sb.ToString();
        }

        /// <summary>
        /// Builds the javascript.
        /// </summary>
        /// <param name="javascriptItemList">The javascript item list.</param>
        /// <returns></returns>
        private string BuildJavascript(List<JavascriptItem> javascriptItemList)
        {
            StringBuilder sb = new StringBuilder();
            if (javascriptItemList != null && javascriptItemList.Count != 0)
            {
                foreach (JavascriptItem javascriptItem in javascriptItemList)
                {
                    sb.Append("\r\t");

                    if (javascriptItem.Type == JavascriptType.Inline)
                    {
                        sb.Append(javascriptItem.Content);
                    }
                    else if (javascriptItem.Type == JavascriptType.External)
                    {
                        sb.Append("<script type=\"text/javascript\" src=\"");
                        sb.Append(base.Resource.BuildJs(javascriptItem.Content));
                        sb.Append("\"></script>");
                    }
                    else if (javascriptItem.Type == JavascriptType.ExternalOtherWeb)
                    {
                        sb.Append("<script type=\"text/javascript\" src=\"");
                        sb.Append(javascriptItem.Content);
                        sb.Append("\"></script>");
                    }
                }
            }
            return sb.ToString();
        }

        #endregion
    }
}
